﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BabyFood.Entities;
using BabyFood.Data;
using System.Data;

public partial class ChiTietVideo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["VideoId"] != null)
            {
                H_VideoId.Value = Request.QueryString["VideoId"].ToString();
                View_Video(Convert.ToInt32(H_VideoId.Value));
            }
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }
    protected void View_Video(int IdVideo)
    {
        Video entVideo = DataRepository.VideoProvider.GetById(IdVideo);
        try
        {
            if (entVideo != null)
            {
                lblTieuDeTin.Text = entVideo.Ten;
                lblNoiDungTin.Text = Server.HtmlDecode(entVideo.DienGiai);
                string testNgay = Common.StandardStringjquery(entVideo.NgayCapNhat.ToString("dddd"));
                lit_video.Text = "<object width=\"585px\" height=\"380px\">";
                lit_video.Text += "<param name=\"movie\" value=" + entVideo.Code +"></param>";
                lit_video.Text += "<param name=\"allowFullScreen\" value=\"true\"></param>";
                lit_video.Text += "<param name=\"allowscriptaccess\" value=\"always\"></param>";                      
                lit_video.Text += "<embed src=" + entVideo.Code +" type=\"application/x-shockwave-flash\" width=\'585px'";
                lit_video.Text += "height=\'350px\' allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed></object>";
                Session["Title"] = entVideo.Ten;
                H_TieuDe.Value = entVideo.Ten;         
                if (testNgay == "Chủ Nhật")
                    lblNgayCapNhat.Text = Common.StandardStringjquery(entVideo.NgayCapNhat.ToString("'' dddd dd/MM/yyyy, HH:mm 'GMT+7'"));
                else
                    lblNgayCapNhat.Text = Common.StandardStringjquery(entVideo.NgayCapNhat.ToString("'Thứ' dddd dd/MM/yyyy, HH:mm 'GMT+7'"));
            }
        }
        catch (Exception ex) { }
    }

    protected void cmdFacebook_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.Redirect("http://www.facebook.com/sharer.php?u=" + HttpContext.Current.Request.Url.AbsolutePath);
        }
        catch (Exception ex)
        { }
    }

    protected void cmdTwiter_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.Redirect("http://twitter.com/home?status=" + HttpContext.Current.Request.Url.AbsolutePath);
        }
        catch (Exception ex)
        { }
    }

    protected void cmdYahoo_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.Redirect("ymsgr:im?+&msg=Xem bài này hay lắm nè: " + HttpContext.Current.Request.Url.AbsolutePath);
        }
        catch (Exception ex)
        { }
    }

    protected void cmdEmail_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.Redirect("https://mail.google.com/mail/?view=cm&fs=1&to&su=" + H_TieuDe.Value + "&body=" + HttpContext.Current.Request.Url.AbsolutePath + "%23.TwF6mDNohJo.gmail&ui=2&tf=1&shva=1");
        }
        catch (Exception ex)
        { }
    }

    protected void cmdZing_Click(object sender, ImageClickEventArgs e)
    {
        try
        {            
            Response.Redirect("http://link.apps.zing.vn/share?u=" + HttpContext.Current.Request.Url.AbsolutePath + "&images=&media=&width=0&height=0");
        }
        catch (Exception ex)
        { }
    }

    protected void cmdIn_Click(object sender, ImageClickEventArgs e)
    {
        Session["ctrl"] = Panel1;
        ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=600px,width=800px,scrollbars=1');</script>");
    }

    protected void cmdSend_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            CaptchaControl1.ValidateCaptcha(txtCaptcha.Text);
            if (CaptchaControl1.UserValidated)
            {
                //CommentVideo entComment = new CommentVideo();
                //entComment.IdVideo = Convert.ToInt32(H_VideoId.Value);
                //entComment.Ten = Common.StandardString(txtName.Text);
                //entComment.Mail = Common.StandardString(txtEmails.Text);
                //entComment.TieuDe = Common.StandardString(txtTieuDe.Text);
                //entComment.TrangThai = true;
                //entComment.NoiDung = Server.HtmlEncode(txtNoiDung.Text);
                //entComment.NgayCapNhat = Common.GetServerDateTime();
                //updateStatus = DataRepository.CommentVideoProvider.Insert(entComment);

                if (!updateStatus)
                    WebMessage.Show("Cập nhật Comment thất bại!");
                else
                {

                    Response.Redirect("~/ChiTietVideo.aspx?VideoId=" + Convert.ToInt32(H_VideoId.Value));
                    WebMessage.Show("Cập nhật Comment thành công!");
                }
                if (updateStatus)
                {
                    txtEmails.Text = "";
                    txtName.Text = "";
                    txtNoiDung.Text = "";
                    txtTieuDe.Text = "";
                    txtCaptcha.Text = "";
                }
                else
                    WebMessage.Show("Cập nhật Comment thất bại!");
            }
            else
            {
                lblResult.Text = "Bạn đã gõ sai mã bảo vệ";
                lblResult.ForeColor = System.Drawing.Color.Red;
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("ChiTietVideo.aspx.cs", "cmdSend_Click", ex.ToString());
            WebMessage.Show("Cập nhật Comment thất bại!");
        }
    }
  
    /// <summary>
    /// Bind Comment
    /// </summary>
    /// <param name="PageNo"></param>
    protected void Bind_Data(int PageNo)
    {
        try
        {
            //facebook.NavigateUrl = "http://www.facebook.com/sharer.php?u=" + Common.sPathUpLienKet + "/TinTucND.aspx?Id="+ H_CatalogId.Value.ToString();
            //twiter.NavigateUrl = "http://twitter.com/home?status=" + Common.sPathUpLienKet + "/TinTucND.aspx?Id="+ H_CatalogId.Value.ToString();
            //zingme.NavigateUrl = "http://wb.me.zing.vn/index.php?wb=" + Common.sPathUpLienKet + "/TinTucND.aspx?Id=" + H_CatalogId.Value.ToString() + "&t=js&c=share_button";
            int TotalRows = 0;
            string WhereClause = "1=1 and IdVideo=" + H_VideoId.Value + "";
            DataSet dsTinTuc = DataRepository.Provider.ExecuteDataSet("CommentVideo_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize);
            dlcomment.DataSource = dsTinTuc.Tables[0];
            dlcomment.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsTinTuc.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }
        catch (Exception)
        {
        }
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        Bind_Data(currnetPageIndex);
    }

    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }
}